package action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;

import service.ProductService;
import service.ProductServiceImpl;
import vo.Product;
import vo.Producttype;

public class ProductAction extends ActionSupport{
	Product product;
	Producttype type;
	ProductService service;
	String key;
	int price1,price2,price3;
	int inputSize;
	List inputdata;
	JSONArray root;
	List list;
	
	//添加产品
	public String addProduct(){
		System.out.println("addaction");
		list = new ArrayList();
		JSONArray input = JSONArray.parseArray(JSON.toJSONString(inputdata,false));
		JSONObject jso=JSON.parseObject(input.getString(0));
		for(int i = 0 ; i < inputSize ; i++){
			product.setProductName(jso.getJSONObject(""+i+"").get("product.productName").toString());
			product.setRemark(jso.getJSONObject(""+i+"").get("product.remark").toString());
			type.setName(jso.getJSONObject(""+i+"").get("type.name").toString());
			price1 = Integer.parseInt(jso.getJSONObject(""+i+"").get("price1").toString());
			price2 = Integer.parseInt(jso.getJSONObject(""+i+"").get("price2").toString());
			price3 = Integer.parseInt(jso.getJSONObject(""+i+"").get("price3").toString());
			list.add(service.addProduct(product,type,price1,price2,price3));
		}
		
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	//删除产品
	public String deleteProduct(){
		list = new ArrayList();
		list.add(service.deleteProduct(product));
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	//修改产品
	public String updateProduct(){
		list = new ArrayList();
		list.add(service.updateProduct(product,price1,price2,price3));
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	//修改产品类型
	public String updateType(){
		list = new ArrayList();
		list.add(service.updateType(type));
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	//查询产品
	public String selectProduct(){
		list = new ArrayList();
		list = service.selectProduct(key);
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		/*System.out.println(root.toString());*/
		return "success";
	}

	public String getAllProduct(){
		list = service.getAllProduct();
		System.out.println(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	//增加产品类型
	public String addProductType(){
		list = new ArrayList();
		JSONArray input = JSONArray.parseArray(JSON.toJSONString(inputdata,false));
		JSONArray input2 = JSONArray.parseArray(input.getString(0));
		System.out.println(inputSize);
		for(int i = 0 ; i < inputSize ; i++){
			JSONObject jso = JSON.parseObject(input2.getString(i));
			type.setName(jso.get("producttype.name").toString());
			list.add(service.addProductType(type));
		}
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String getProductType(){
		List<Producttype> list2 = new ArrayList<Producttype>();
		list2 = service.getProductType();
		root = JSONArray.parseArray(JSON.toJSONString(list2,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String deleteProductType(){
		list = new ArrayList();
		list.add(service.deleteProductType(type.getId()));
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String searchProductType(){
		/*System.out.println(key);*/
		list = service.searchProductType(key);
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String getProductByType(){
		List<Product> list2 = new ArrayList<Product>();
		list2 = service.getProductByType(type);
		/*System.out.println(list2.get(0).getProductName());*/
		root = JSONArray.parseArray(JSON.toJSONString(list2,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Producttype getType() {
		return type;
	}

	public void setType(Producttype type) {
		this.type = type;
	}

	public ProductService getService() {
		return service;
	}

	public void setService(ProductService service) {
		this.service = service;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public JSONArray getRoot() {
		return root;
	}

	public void setRoot(JSONArray root) {
		this.root = root;
	}

	public int getPrice1() {
		return price1;
	}

	public void setPrice1(int price1) {
		this.price1 = price1;
	}

	public int getPrice2() {
		return price2;
	}

	public void setPrice2(int peice2) {
		this.price2 = peice2;
	}

	public int getPrice3() {
		return price3;
	}

	public void setPrice3(int price3) {
		this.price3 = price3;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public List getInputdata() {
		return inputdata;
	}

	public void setInputdata(List inputdata) {
		this.inputdata = inputdata;
	}

	public int getInputSize() {
		return inputSize;
	}

	public void setInputSize(int inputSize) {
		this.inputSize = inputSize;
	}

}
